﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Web.Syndication;

namespace Mutzl.Samples.VideoApp.Player_C_YouTube.Model
{
    public class Tweet : ObservableObject
    {
        public string Name { get; set; }
        public string Handle { get; set; }
        public string Message { get; set; }
        public ImageSource Image { get; set; }
        public DateTime Published { get; set; }
        public Uri Link { get; set; }

        public async static Task<IEnumerable<Tweet>> GetTweetsAsync(string topic)
        {
            //Dokumentation der Twitter API
            //https://dev.twitter.com/docs/api/1    
            //https://dev.twitter.com/docs/api/1.1

            string rssUri = string.Format("http://search.twitter.com/search.atom?q={0}", topic);
            return await GetTweetsAsync(new Uri(rssUri, UriKind.Absolute));
        }

        public async static Task<IEnumerable<Tweet>> GetTweetsAsync(Uri rssUri)
        {
            // Todo: C05. Hier gegebenenfalls Parsing der Tweets anpassen.
            // Daten-Sample siehe \TwitterRss.xml

            try
            {
                var client = new SyndicationClient();

                SyndicationFeed feed = await client.RetrieveFeedAsync(rssUri);

                var regex = new Regex(@"(?<Handle>.*)\s\((?<Name>.*)\)");

                var tweets = new List<Tweet>();

                foreach (var item in feed.Items)
                {
                    var author = regex.Match(item.Authors[0].Name).Groups;
                    var tweet = new Tweet()
                    {
                        Name = author["Name"].Value,
                        Handle = "@" + author["Handle"].Value,
                        Message = item.Content.Text.Replace("<em>", "").Replace("</em>", ""),
                        Image = new BitmapImage(item.Links[1].Uri),
                        Published = item.PublishedDate.DateTime,
                        Link = item.Links[0].Uri,
                    };

                    tweets.Add(tweet);
                }

                return tweets;
            }
            catch
            {
                return null;
            }
        }

        // wird im Design-Modus geladen, damit Layout in Blend und Visual Studio sichtbar ist.
        public static Tweet CreateDesignTweet()
        {
            return new Tweet()
            {
                Name = "Thomas Mutzl",
                Handle = "@mutzl",
                Message = @"Working on another Win8 app including <a href=""https://twitter.com/"">Twitter</a>. Guess what this tweet is supposed to be for...",
                Image = new BitmapImage(new Uri("ms-appx:///Assets/demo_twitter.jpg")),
                Published = new DateTime(2012,11,19,21,12,0),
                Link = new Uri("https://twitter.com/mutzl/statuses/270635647310626818"),
            };
        }
    }
}
